{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a59ac003-6462-4523-b5f4-090516b69aa7",
   "metadata": {},
   "source": [
    "## 不同的Agent类型\n",
    "- OPENAI_FUNCTIONS：openai函数调用型\n",
    "- ZERO_SHOT_REACT_DESCRIPTION：零样本增强生成型\n",
    "- CHAT_ZERO_SHOT_REACT_DESCRIPTION：零样本增强生成型(对话)\n",
    "- CONVERSATIONAL_REACT_DESCRIPTION：对话增强生成型\n",
    "- STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION：结构化对话生成增强型\n",
    "\n",
    "内置的不同类型，实际是不同的提示器模板"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "638eb37d-470e-4428-9215-26dbc16f6584",
   "metadata": {},
   "source": [
    "### ZERO_SHOT_REACT_DESCRIPTION\n",
    "零样本增强式生成,即在没有示例的情况下可以自主的进行对话的类型。\n",
    "https://blog.csdn.net/zcyjava/article/details/127006287 [零样本、单样本、少样本]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "79b8220e-6e24-4fe7-b04e-0aa302b83b56",
   "metadata": {},
   "outputs": [],
   "source": [
    "# serpapi 是一个聚合的搜索引擎，需要安装google搜索包及申请key https://serpapi.com/manager-api-key\n",
    "# llm-math 是封装好的数学计算链\n",
    "! pip install google-search-reuslts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d9118f95-09b0-40d4-b16c-eb8723a012e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.llms import OpenAI\n",
    "from langchain.agents import (\n",
    "    load_tools,\n",
    "    initialize_agent,\n",
    "    AgentType,\n",
    ")\n",
    "import os\n",
    "os.environ[\"SERPAPI_API_KEY\"] = \"f265b8d9834ed7692cba6db6618e2a8a9b24ed6964c457296a2626026e8ed594\"\n",
    "\n",
    "llm = OpenAI(\n",
    "    temperature=0,\n",
    "    model=\"gpt-3.5-turbo-instruct\",\n",
    ")\n",
    "tools = load_tools([\"serpapi\",\"llm-math\"],llm=llm)\n",
    "agent = initialize_agent(\n",
    "    tools,\n",
    "    llm,\n",
    "    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,\n",
    "    verbose=True,\n",
    ")\n",
    "\n",
    "print(agent)\n",
    "print(agent.agent.llm_chain.prompt.template)\n",
    "agent.invoke(\"现在美国总统是谁？他的年龄除以2是多少？\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29724cb2-6a66-4322-897a-61ba4e936637",
   "metadata": {},
   "source": [
    "### CHAT_ZERO_SHOT_REACT_DESCRIPTION \n",
    "使用了chatmodel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0554c814-7356-4660-b30e-2e7b3305e37f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.chat_models import ChatOpenAI\n",
    "from langchain.agents import (\n",
    "    load_tools,\n",
    "    initialize_agent,\n",
    "    AgentType,\n",
    ")\n",
    "import os\n",
    "os.environ[\"SERPAPI_API_KEY\"] = \"f265b8d9834ed7692cba6db6618e2a8a9b24ed6964c457296a2626026e8ed594\"\n",
    "\n",
    "llm = ChatOpenAI(\n",
    "    temperature=0,\n",
    "    model=\"gpt-3.5-turbo\",\n",
    ")\n",
    "tools = load_tools([\"serpapi\",\"llm-math\"],llm=llm)\n",
    "agent = initialize_agent(\n",
    "    tools,\n",
    "    llm,\n",
    "    agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION,\n",
    "    verbose=True,\n",
    ")\n",
    "\n",
    "print(agent)\n",
    "print(agent.agent.llm_chain.prompt.template)\n",
    "agent.invoke(\"现在美国总统是谁？他的年龄除以2是多少？\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a042fd6-169b-48f4-9330-1bcf6228735c",
   "metadata": {},
   "source": [
    "### CONVERSATIONAL_REACT_DESCRIPTION \n",
    "一个对话型的agent，这个agent要求与memory一起使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c3c5d086-e794-4dcc-943a-747f951e93c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.llms import OpenAI\n",
    "from langchain.agents import (\n",
    "    load_tools,\n",
    "    initialize_agent,\n",
    "    AgentType,\n",
    ")\n",
    "from langchain.memory import ConversationBufferMemory\n",
    "import os\n",
    "os.environ[\"SERPAPI_API_KEY\"] = \"f265b8d9834ed7692cba6db6618e2a8a9b24ed6964c457296a2626026e8ed594\"\n",
    "\n",
    "#记忆组件\n",
    "memory = ConversationBufferMemory(\n",
    "    memory_key=\"chat_history\",\n",
    ")\n",
    "\n",
    "llm = OpenAI(\n",
    "    temperature=0,\n",
    "    model=\"gpt-3.5-turbo-instruct\",\n",
    ")\n",
    "tools = load_tools([\"serpapi\",\"llm-math\"],llm=llm)\n",
    "agent = initialize_agent(\n",
    "    tools,\n",
    "    llm,\n",
    "    agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION,\n",
    "    memory=memory,#记忆组件\n",
    "    verbose=True,\n",
    ")\n",
    "\n",
    "print(agent)\n",
    "print(agent.agent.llm_chain.prompt.template)\n",
    "\n",
    "agent.run(\"hi i am tomie\")\n",
    "agent.run(\"what is my name?\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1d5a93e-f868-43a7-a9e6-07869a011532",
   "metadata": {},
   "source": [
    "### CHAT_CONVERSATIONAL_REACT_DESCRIPTION \n",
    "使用了chatmodel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0cd2bb27-eb18-4a00-ad13-5b740bb7cb3c",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.chat_models import ChatOpenAI\n",
    "from langchain.agents import (\n",
    "    load_tools,\n",
    "    initialize_agent,\n",
    "    AgentType,\n",
    ")\n",
    "from langchain.memory import ConversationBufferMemory\n",
    "import os\n",
    "os.environ[\"SERPAPI_API_KEY\"] = \"f265b8d9834ed7692cba6db6618e2a8a9b24ed6964c457296a2626026e8ed594\"\n",
    "\n",
    "#记忆组件\n",
    "memory = ConversationBufferMemory(\n",
    "    memory_key=\"chat_history\",\n",
    "    return_messages=True,\n",
    ")\n",
    "\n",
    "llm = ChatOpenAI(\n",
    "    temperature=0,\n",
    "    model=\"gpt-4-1106-preview\",\n",
    ")\n",
    "tools = load_tools([\"serpapi\",\"llm-math\"],llm=llm)\n",
    "agent = initialize_agent(\n",
    "    tools,\n",
    "    llm,\n",
    "    agent=AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION,\n",
    "    memory=memory,#记忆组件\n",
    "    verbose=True,\n",
    ")\n",
    "\n",
    "print(agent)\n",
    "print(agent.agent.llm_chain.prompt.template)\n",
    "\n",
    "agent.run(\"hi i am tomie\")\n",
    "agent.run(\"what is my name?\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f46b2379-c029-4d16-b0c4-b1701002b95f",
   "metadata": {},
   "source": [
    "### OPENAI_FUNCTIONS\n",
    "使用openai的函数调用来实现的，只支持openai模型。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7589ef31-c382-49c6-8682-88764995f28d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.chat_models import ChatOpenAI\n",
    "from langchain.agents import (\n",
    "    load_tools,\n",
    "    initialize_agent,\n",
    "    AgentType,\n",
    ")\n",
    "from langchain.memory import ConversationBufferMemory\n",
    "import os\n",
    "os.environ[\"SERPAPI_API_KEY\"] = \"f265b8d9834ed7692cba6db6618e2a8a9b24ed6964c457296a2626026e8ed594\"\n",
    "\n",
    "#记忆组件\n",
    "memory = ConversationBufferMemory(\n",
    "    memory_key=\"chat_history\",\n",
    "    return_messages=True,\n",
    ")\n",
    "\n",
    "llm = ChatOpenAI(\n",
    "    temperature=0,\n",
    "    model=\"gpt-4-1106-preview\",\n",
    ")\n",
    "tools = load_tools([\"serpapi\",\"llm-math\"],llm=llm)\n",
    "agent = initialize_agent(\n",
    "    tools,\n",
    "    llm,\n",
    "    agent=AgentType.OPENAI_FUNCTIONS,\n",
    "    memory=memory,#记忆组件\n",
    "    verbose=True,\n",
    ")\n",
    "\n",
    "print(agent)\n",
    "print(agent.agent.prompt.messages)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cd56bb01-4200-4d9a-9563-05860b048632",
   "metadata": {},
   "source": [
    "### STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION \n",
    "对输出做了结构化处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1cbfe0f7-d82c-4ffe-b999-835880d0a484",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.chat_models import ChatOpenAI\n",
    "from langchain.agents import (\n",
    "    load_tools,\n",
    "    initialize_agent,\n",
    "    AgentType,\n",
    ")\n",
    "from langchain.memory import ConversationBufferMemory\n",
    "import os\n",
    "\n",
    "os.environ[\"SERPAPI_API_KEY\"] = \"f265b8d9834ed7692cba6db6618e2a8a9b24ed6964c457296a2626026e8ed594\"\n",
    "\n",
    "#记忆组件\n",
    "memory = ConversationBufferMemory(\n",
    "    memory_key=\"chat_history\",\n",
    "    return_messages=True,\n",
    ")\n",
    "\n",
    "llm = ChatOpenAI(\n",
    "    temperature=0,\n",
    "    model=\"gpt-4-1106-preview\",\n",
    ")\n",
    "tools = load_tools([\"serpapi\",\"llm-math\"],llm=llm)\n",
    "agent = initialize_agent(\n",
    "    tools,\n",
    "    llm,\n",
    "    agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,\n",
    "    memory=memory,#记忆组件\n",
    "    handle_parsing_errors=True,\n",
    "    verbose=True,\n",
    ")\n",
    "print(agent)\n",
    "print(agent.agent.llm_chain.prompt.messages[0].prompt.template)\n",
    "print(agent.agent.llm_chain.prompt.messages[1].prompt.template)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python (langchain)",
   "language": "python",
   "name": "langchain-env"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
